const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navHeight:'',
    searchType: ["找任务", "找商城", "找服务商"],
    searchTypeShow:false,
    searchTypeContent:"找任务",
    searchTypeIndex:0,
    historyrecordList:[],//历史记录
    historyrecordContent:'',//点击历史记录后获取点击数据
    talentCategoryIndex: 0,
    filterBoxShow: false,
    filterType: 0,
    taskList: [],//找人才数据列表
    serviceList:[],//找服务数据列表
    shopList:[],//找作品数量列表
    historyRecordShow:true,
    searchVal:'',
    pageNum:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this=this
    if (wx.getStorageSync('searchRecording')){
      this.setData({
        historyrecordList: wx.getStorageSync('searchRecording')
      })
    }
    wx.getSystemInfo({
      success(res){
        _this.setData({
          navHeight: res.statusBarHeight
        })
      }
    })
  },
  // 搜索类型展示
  searchTypeText:function(){
    var searchTypeShow = this.data.searchTypeShow
    this.setData({
      searchTypeShow: !searchTypeShow
    })
  },
  // 选中搜索类型
  searchTypeBtn:function(e){
    var searchType = this.data.searchType
    var { index } = e.target.dataset
    this.setData({
      searchTypeContent: searchType[index],
      searchTypeIndex:index,
      searchTypeShow:false
    })
  },
  // 选中的历史记录
  historyRecordBtn:function(e){
    var { index } = e.target.dataset
    var historyrecordList = this.data.historyrecordList
    this.setData({
      historyrecordContent: historyrecordList[index]
    })
  },
  // 删除历史记录
  delHistoryRecord:function(e){
    var { index } = e.target.dataset
    var historyrecordList = this.data.historyrecordList
    historyrecordList.splice(index,1)
    wx.setStorageSync('searchRecording', historyrecordList)
    this.setData({
      historyrecordList
    })
  },
  // 清楚历史数据
  clearHistoryRecord:function(){
    this.setData({
      historyrecordList:''
    })
    wx.removeStorageSync('searchRecording')
  },
  // 分类切换
  talentCategoryTab: function (e) {
    var { index } = e.currentTarget.dataset
    this.setData({
      talentCategoryIndex: index
    })
  },
  // 分类列表显示隐藏
  categoryBtn: function () {
    var { filterBoxShow } = this.data
    this.setData({
      filterBoxShow: !filterBoxShow,
      filterType: 0
    })
  },
  // 排序列表显示隐藏
  sortBtn: function () {
    var { filterBoxShow } = this.data
    this.setData({
      filterBoxShow: !filterBoxShow,
      filterType: 1
    })
  },
  // 获取搜索内容
  searchInp:function(e){
    this.setData({
      searchVal: e.detail.value
    })
  },
  // 搜索
  searchBtn:function(e){
    this.setData({
      searchVal: e.detail
    })
    var _this = this;
    var { searchVal, pageNum, searchTypeIndex } = this.data;
    if (searchVal == ''){
      wx.showToast({
        title: '请输入搜索关键词',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    wx.showLoading({
      title: '加载中',
    })
    // var searchRecordingObj = [];
    // var getSearchRecording = wx.getStorageSync('searchRecording')
    // if (getSearchRecording){
    //   searchRecordingObj = getSearchRecording
    //   searchRecordingObj.push(searchVal)
    //   wx.setStorageSync('searchRecording', searchRecordingObj)
    // }else{
    //   searchRecordingObj.push(searchVal)
    //   wx.setStorageSync('searchRecording', searchRecordingObj)
    // }
    // 搜索请求
    wx.request({
      url: app.globalData.baseUrl + 'index/search',
      data: {
        type: searchTypeIndex + 1,
        content: searchVal,
        page: pageNum
      },
      success(res) {
        wx.hideLoading()
        console.log(res)
        if (res.data.code == 1000) {
          _this.setData({
            historyRecordShow:false
          })
          if (searchTypeIndex == 0){
            _this.setData({
              taskList: res.data.data.data
            })
          } else if (searchTypeIndex == 1){
            _this.setData({
              shopList: res.data.data.data
            })
          } else {
            _this.setData({
              serviceList: res.data.data
            })
          }
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  // 点击任务
  taskDetail: function (e) {
    var { id } = e.currentTarget.dataset;
    wx.navigateTo({
      url: '/taskPage/pages/taskDetail/taskDetail?id=' + id,
    })
  },
  // 点击商城列表
  shopList: function (e) {
    var { id,type } = e.currentTarget.dataset;
    wx.navigateTo({
      url: '/taskPage/pages/worksDetail/worksDetail?id=' + id + '&type=' + type,
    })
  },

  // 点击列表
  talentListBtn:function(e){
    var { id,uid,shop_status } = e.currentTarget.dataset;
    var shopType = 1;
    if(shop_status != 1){
      id = uid,
          shopType = 2
    }
    wx.navigateTo({
      url: '/taskPage/pages/talentDetail/talentDetail?id=' + id + '&shopType=' + shopType,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
